using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class ComponentMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.cpnt_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	public ComponentMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnMenuClose += OnMenuClose;
			menu.OnItemSelect += OnItemSelect;
			isMenuInitialized = true;
		}
	}

	public static void InitComponentMenu()
	{
		menu.Clear();
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Face));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Head));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Hair));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Torso1));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Torso2));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Torso3));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Legs));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Hands));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Feet));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Accessories));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Tasks));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Textures));
		for (int i = 0; i < 12; i++)
		{
			menu.BindMenuToItem(ComponentSubMenu.menu, menu.MenuItems[i]);
		}
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Randomize));
		menu.AddItem(new UIMenuItem(TextSetting.cpnt_MenuItem_Default));
		menu.RefreshIndex();
		CheckCanUseMenuItem();
	}

	private static int MenuIndexToCid(int menuIndex)
	{
		return menuIndex switch
		{
			0 => 0, 
			1 => 1, 
			2 => 2, 
			3 => 3, 
			4 => 11, 
			5 => 8, 
			6 => 4, 
			7 => 5, 
			8 => 6, 
			9 => 7, 
			10 => 9, 
			11 => 10, 
			_ => menuIndex, 
		};
	}

	private static int CidToMenuIndex(int cid)
	{
		return cid switch
		{
			0 => 0, 
			1 => 1, 
			2 => 2, 
			3 => 3, 
			11 => 4, 
			8 => 5, 
			4 => 6, 
			5 => 7, 
			6 => 8, 
			7 => 9, 
			9 => 10, 
			10 => 11, 
			_ => cid, 
		};
	}

	private static void CheckCanUseMenuItem()
	{
		Skin.Component[] components;
		int[][] componentsTextureData;
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.skin.GetComponentsValue();
			Main.P2.skin.GetComponentsVariableNumber();
			components = Main.P2.skin.Components;
			componentsTextureData = Main.P2.skin.ComponentsTextureData;
		}
		else
		{
			Main.P1.skin.GetComponentsValue();
			Main.P1.skin.GetComponentsVariableNumber();
			Main.Clone.skin.GetComponentsValue();
			Main.Clone.skin.GetComponentsVariableNumber();
			components = Main.P1.skin.Components;
			componentsTextureData = Main.P1.skin.ComponentsTextureData;
		}
		for (int i = 0; i < menu.MenuItems.Count - 2; i++)
		{
			menu.MenuItems[i].Enabled = false;
		}
		for (int j = 0; j < menu.MenuItems.Count - 2; j++)
		{
			if (components[j].drawableNumber > 1 || (components[j].drawableNumber == 1 && componentsTextureData[j][0] > 1))
			{
				menu.MenuItems[CidToMenuIndex(j)].Enabled = true;
			}
		}
	}

	public static void RandomizeComponents()
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.Character.RandomizeOutfit();
			Main.P2.skin.GetComponentsValue();
			Main.P2.skin.GetComponentsVariableNumber();
			for (int i = 0; i < Main.P2.skin.Components.Length; i++)
			{
				Main.P2.skin.Components[i].action = true;
			}
			return;
		}
		Main.P1.Character.RandomizeOutfit();
		Main.P1.skin.GetComponentsValue();
		Main.P1.skin.GetComponentsVariableNumber();
		Main.Clone.skin.Components = Main.P1.skin.Components;
		for (int j = 0; j < Main.Clone.skin.Components.Length; j++)
		{
			SkinHelper.SetPedComponentVariation(Main.Clone.Character, j, Main.Clone.skin.Components[j].drawableId, Main.Clone.skin.Components[j].textureId, Main.Clone.skin.Components[j].paletteId);
		}
		for (int k = 0; k < Main.P1.skin.Components.Length; k++)
		{
			Main.P1.skin.Components[k].action = true;
			Main.Clone.skin.Components[k].action = true;
		}
	}

	public static void DefaultComponents()
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.Character.SetDefaultClothes();
			Main.P2.skin.GetComponentsValue();
			Main.P2.skin.GetComponentsVariableNumber();
			for (int i = 0; i < Main.P2.skin.Components.Length; i++)
			{
				Main.P2.skin.Components[i].action = false;
			}
			return;
		}
		Main.P1.Character.SetDefaultClothes();
		Main.Clone.Character.SetDefaultClothes();
		Main.P1.skin.GetComponentsValue();
		Main.P1.skin.GetComponentsVariableNumber();
		Main.Clone.skin.Components = Main.P1.skin.Components;
		for (int j = 0; j < Main.P1.skin.Components.Length; j++)
		{
			Main.P1.skin.Components[j].action = false;
			Main.Clone.skin.Components[j].action = false;
		}
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		if (index >= 0 && index <= 11)
		{
			ComponentSubMenu.InitComponentSubMenu(MenuIndexToCid(index));
		}
		if (index == 12)
		{
			RandomizeComponents();
		}
		if (index == 13)
		{
			DefaultComponents();
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
